<template>
	<view class="mine-container">
		<!-- 用户信息卡片 -->
		<view class="user-card">
			<view class="user-info">
				<image class="avatar" :src="userInfo.avatar || '/static/avatar/default-avatar.png'" mode="aspectFill" />
				<view class="info-content" @tap="goToLogin" v-if="!userInfo.isLogin">
					<text class="nickname">点击登录</text>
					<text class="desc">登录后享受更多服务</text>
				</view>
				<view class="info-content" v-else>
					<text class="nickname">{{ userInfo.nickname }}</text>
					<text class="desc">{{ userInfo.desc }}</text>
				</view>
				<view class="setting-icon" @tap="goToSetting">
					<text class="icon">⚙️</text>
				</view>
			</view>
			<!-- 用户数据概览 -->
			<view class="user-stats">
				<view class="stat-item" v-for="(item, index) in userStats" :key="index" @tap="goToDetail(item.type)">
					<text class="num">{{ item.num }}</text>
					<text class="label">{{ item.label }}</text>
				</view>
			</view>
		</view>

		<!-- 功能列表 -->
		<view class="feature-list">
			<view class="feature-group" v-for="(group, groupIndex) in featureList" :key="groupIndex">
				<view 
					class="feature-item" 
					v-for="(item, index) in group" 
					:key="index"
					@tap="handleFeature(item.type)"
				>
					<view class="feature-icon" :style="{ backgroundColor: item.bgColor }">
						<text class="icon">{{ item.icon }}</text>
					</view>
					<view class="feature-info">
						<text class="feature-name">{{ item.name }}</text>
						<text class="arrow">›</text>
					</view>
				</view>
			</view>
		</view>

		<!-- 退出登录按钮 -->
		<button class="logout-btn" v-if="userInfo.isLogin" @tap="handleLogout">退出登录</button>

		<!-- 添加登录弹窗 -->
		<login-popup 
			v-model="showLogin"
			@success="handleLoginSuccess"
		/>
	</view>
</template>

<script setup>
import { ref } from 'vue'
import LoginPopup from '@/components/LoginPopup.vue'
import { userApi } from '@/utils/api'

// 用户信息
const userInfo = ref({
	isLogin: false,
	avatar: '',
	nickname: '未登录',
	desc: '登录后享受更多服务'
})

// 用户统计数据
const userStats = ref([
	{ type: 'course', num: '0', label: '我的课程' },
	{ type: 'collect', num: '0', label: '收藏' },
	{ type: 'point', num: '0', label: '积分' }
])

// 功能列表
const featureList = ref([
	[
		{ 
			type: 'learning',
			name: '学习规划', 
			icon: '📚',
			bgColor: '#007aff20'
		},
		{ 
			type: 'download',
			name: '离线缓存', 
			icon: '📥',
			bgColor: '#52c41a20'
		},
		{
			type: 'notes',
			name: 'BI分析',
			icon: '📝',
			bgColor: '#ff6b6b20'
		}
	],
	[
		{
			type: 'feedback',
			name: '意见反馈',
			icon: '💡',
			bgColor: '#faad1420'
		},
		{
			type: 'about',
			name: '关于我们',
			icon: '🏢',
			bgColor: '#13c2c220'
		}
	]
])

// 显示登录弹窗
const showLogin = ref(false)

// 跳转到登录页修改为显示弹窗
const goToLogin = () => {
	if (!userInfo.value.isLogin) {
		showLogin.value = true
	}
}

// 跳转到设置页
const goToSetting = () => {
	uni.navigateTo({
		url: '/pages/setting/setting'
	})
}

// 处理功能点击
const handleFeature = (type) => {
	switch (type) {
		case 'learning':
			uni.navigateTo({ url: '/pages/learning/plan' })
			break
		case 'download':
			uni.navigateTo({ url: '/pages/download/list' })
			break
		case 'notes':
			uni.navigateTo({ url: '/pages/notes/list' })
			break
		case 'feedback':
			uni.navigateTo({ url: '/pages/feedback/index' })
			break
		case 'about':
			uni.navigateTo({ url: '/pages/about/index' })
			break
	}
}

// 跳转到详情页
const goToDetail = (type) => {
	const urls = {
		course: '/pages/mine/course',
		collect: '/pages/mine/collect',
		point: '/pages/mine/point'
	}
	if (urls[type]) {
		uni.navigateTo({ url: urls[type] })
	}
}

// 退出登录
const handleLogout = () => {
	uni.showModal({
		title: '提示',
		content: '确定要退出登录吗？',
		success: async (res) => {
			if (res.confirm) {
				try {
					// 显示加载提示
					uni.showLoading({
						title: '退出中...'
					})
					
					// 调用退出登录接口
					await userApi.logout()
					
					// 清除所有存储的信息
					uni.removeStorageSync('JSESSIONID')
					uni.removeStorageSync('userInfo')
					uni.removeStorageSync('studyRecords')
					uni.removeStorageSync('currentCourse')
					uni.removeStorageSync('currentChapter')
					
					// 重置用户信息
					userInfo.value = {
						isLogin: false,
						avatar: '',
						nickname: '未登录',
						desc: '登录后享受更多服务'
					}
					
					// 重置统计数据
					userStats.value = [
						{ type: 'course', num: '0', label: '我的课程' },
						{ type: 'collect', num: '0', label: '收藏' },
						{ type: 'point', num: '0', label: '积分' }
					]
					
					uni.showToast({
						title: '已退出登录',
						icon: 'success'
					})
					
					// 跳转到首页
					uni.switchTab({
						url: '/pages/index/index'
					})
				} catch (error) {
					console.error('退出登录失败:', error)
					uni.showToast({
						title: '退出失败，请重试',
						icon: 'none'
					})
				} finally {
					uni.hideLoading()
				}
			}
		}
	})
}

// 处理登录成功
const handleLoginSuccess = (info) => {
	userInfo.value = info
	// 刷新用户数据
	userStats.value = [
		{ type: 'course', num: '3', label: '我的课程' },
		{ type: 'collect', num: '1', label: '收藏' },
		{ type: 'point', num: '100', label: '积分' }
	]
}
</script>

<style>
page {
	background-color: #f6f7f9;
}
</style>

<style scoped>
.mine-container {
	min-height: 100vh;
	padding: 30rpx;
	padding-bottom: 60rpx;
}

.user-card {
	background: linear-gradient(135deg, #007aff 0%, #0056b3 100%);
	border-radius: 24rpx;
	padding: 40rpx 30rpx;
	color: #fff;
	box-shadow: 0 4rpx 16rpx rgba(0, 122, 255, 0.2);
}

.user-info {
	display: flex;
	align-items: center;
	position: relative;
}

.avatar {
	width: 120rpx;
	height: 120rpx;
	border-radius: 60rpx;
	border: 4rpx solid rgba(255, 255, 255, 0.3);
}

.info-content {
	flex: 1;
	margin-left: 30rpx;
}

.nickname {
	font-size: 36rpx;
	font-weight: 500;
	margin-bottom: 8rpx;
	display: block;
}

.desc {
	font-size: 24rpx;
	opacity: 0.8;
}

.setting-icon {
	padding: 20rpx;
	position: absolute;
	right: 0;
	top: 0;
}

.icon {
	font-size: 40rpx;
}

.user-stats {
	display: flex;
	justify-content: space-around;
	margin-top: 40rpx;
	padding-top: 30rpx;
	border-top: 2rpx solid rgba(255, 255, 255, 0.1);
}

.stat-item {
	text-align: center;
}

.num {
	font-size: 36rpx;
	font-weight: bold;
	display: block;
	margin-bottom: 8rpx;
}

.label {
	font-size: 24rpx;
	opacity: 0.8;
}

.feature-list {
	margin-top: 30rpx;
}

.feature-group {
	background-color: #fff;
	border-radius: 24rpx;
	margin-bottom: 20rpx;
	padding: 0 20rpx;
	box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.05);
}

.feature-item {
	display: flex;
	align-items: center;
	padding: 30rpx 20rpx;
	border-bottom: 2rpx solid #f5f5f5;
}

.feature-item:last-child {
	border-bottom: none;
}

.feature-icon {
	width: 80rpx;
	height: 80rpx;
	border-radius: 20rpx;
	display: flex;
	align-items: center;
	justify-content: center;
	margin-right: 20rpx;
}

.feature-info {
	flex: 1;
	display: flex;
	align-items: center;
	justify-content: space-between;
}

.feature-name {
	font-size: 28rpx;
	color: #333;
}

.arrow {
	font-size: 36rpx;
	color: #999;
	font-family: serif;
}

.logout-btn {
	margin-top: 60rpx;
	background-color: #fff;
	color: #ff4d4f;
	font-size: 28rpx;
	border-radius: 20rpx;
	box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.05);
}

.logout-btn:active {
	opacity: 0.8;
}
</style> 